package com.ciccwm.core.filter;

import com.ciccwm.core.exception.BusinessException;
import com.ciccwm.core.exception.ValidationException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.RpcException;

/**
 * Dubbo异常处理器.
 */
@Slf4j
@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER})
public class DubboExceptionHandler {
    public static RpcException handleException(Throwable bizExcp) {
        if (bizExcp instanceof BusinessException) {
            log.warn("Business exception occurred: {}", bizExcp.getMessage());
            return new RpcException(RpcException.BIZ_EXCEPTION, bizExcp.getMessage());
        } else if (bizExcp instanceof ValidationException) {
            log.warn("Validation exception occurred: {}", bizExcp.getMessage());
            return new RpcException(RpcException.VALIDATION_EXCEPTION, bizExcp.getMessage());
        } else if (bizExcp instanceof RpcException) {
            log.error("RPC exception occurred", bizExcp);
            return (RpcException)bizExcp ;
        } else {
            log.error("Unexpected exception occurred", bizExcp);
            return new RpcException(RpcException.UNKNOWN_EXCEPTION, "Internal server error");
        }
    }
} 
